home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sphigs.lha / SPHIGS / examples / viewing_demo.MAConly / SPHDEMO_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-26  |  15.7 KB  |  486 lines

  1. #include "HEADERS.h"
  2. #include "SPHDEMO.h"
  3. #include "SPHDEMO_struct.h"
  4. #include "SPHDEMO_view.h"
  5.  
  6. #include <MenuMgr.h>
  7.  
  8. extern WindowPtr srgpmac__cwindow;
  9.  
  10. static MenuHandle rendermenu, chimneymenu, cameramenu, viewportmenu, pickmenu,
  11.               gridmenu, applemenu, modelmenu; 
  12. static int currenderitem = 6, curchimneymenuitem = 3;
  13. static int curgridmenuitem = 1, curvpmenuitem = 2;
  14. static int curCamDialogID = 0;  /* resource ID of camera dialog box currently in use */
  15. static DialogPtr curCamDialogPtr;
  16.  
  17. static int curHelpDialogID = 0;
  18. static DialogPtr curHelpDialogPtr;
  19.  
  20. #define granDialogID 666
  21. static DialogPtr granDialogPtr;   /* granularity dialog box */
  22. static Handle    grantextitemhandle, tmpitemhandle;
  23. static DialogPtr tmpdptr;
  24.  
  25. #define rendermenuID   600
  26. #define cameramenuID   602
  27. #define chimneymenuID  601
  28. #define viewportmenuID 604
  29. #define pickmenuID     605
  30. #define gridmenuID     603
  31. #define modelmenuID    606
  32. #define applemenuID    609
  33.  
  34. #define HelpDlogFor(MENUID)   (3600+MENUID)
  35.  
  36. static boolean stillVirgin = TRUE;
  37.  
  38.  
  39. static void InstallCameraDialog (int whichitem)
  40. {
  41.    /* UNMAP ANY EXTANT DIALOG BOX */
  42.    if (curCamDialogID > 0)
  43.       DisposDialog (curCamDialogPtr);
  44.       
  45.    /* UNCHECK ITEM IN MENU */
  46.    if (curCamDialogID > 0)
  47.       CheckItem (cameramenu, curCamDialogID-300, FALSE);
  48.       
  49.    /* MAP NEW DIALOG BOX */
  50.    curCamDialogID = whichitem + 300;
  51.    curCamDialogPtr = GetNewDialog (curCamDialogID, NULL, (WindowPtr)-1);
  52.    DrawDialog (curCamDialogPtr);
  53.    CheckItem (cameramenu, whichitem, TRUE);
  54. }
  55.  
  56. static void InstallHelpDialog (int whichmenuid)
  57. {
  58.    /* UNMAP ANY EXTANT DIALOG BOX */
  59.    if (curHelpDialogID > 0)
  60.       DisposDialog (curHelpDialogPtr);
  61.       
  62.    /* MAP NEW DIALOG BOX */
  63.    curHelpDialogID = HelpDlogFor(whichmenuid);
  64.    curHelpDialogPtr = GetNewDialog (curHelpDialogID, NULL, (WindowPtr)-1);
  65.    DrawDialog (curHelpDialogPtr);
  66. }
  67.  
  68.  
  69.  
  70.       
  71. static int motion_granularity = 30;
  72.          
  73. void DisplayMotionGranularity (void)
  74. {
  75.    char buffer[19];
  76.    sprintf (buffer, "%d", motion_granularity);
  77.    CtoPstr(buffer);
  78.    SetIText (grantextitemhandle, buffer);
  79. }
  80.  
  81.  
  82.  
  83. void AddGridInFrontOfRoot (int structID, int viewID)
  84. {
  85.                   SPH_unpostRoot (structID, viewID);
  86.                   SPH_postRoot (GRID_STRUCT, viewID);
  87.                   SPH_postRoot (structID, viewID);
  88. }
  89.  
  90.  
  91. void MakeMenuBar()
  92. {
  93.    InsertMenu (applemenu=GetMenu(applemenuID), 0); 
  94.    InsertMenu (rendermenu=GetMenu(rendermenuID), 0); 
  95.    InsertMenu (cameramenu=GetMenu(cameramenuID), 0);
  96.    InsertMenu (viewportmenu=GetMenu(viewportmenuID), 0);
  97.    InsertMenu (pickmenu=GetMenu(pickmenuID), 0);
  98.    InsertMenu (gridmenu=GetMenu(gridmenuID), 0);
  99.    InsertMenu (chimneymenu=GetMenu(chimneymenuID), 0);
  100.    InsertMenu (modelmenu=GetMenu(modelmenuID), 0);
  101.    DrawMenuBar();
  102. }
  103.  
  104.  
  105. void ReportSpecialError (char *message, boolean is_fatal);
  106.  
  107.  
  108. main()
  109. {
  110.     EventRecord ev;
  111.     WindowPtr winptr;
  112.     long result;
  113.     int whichmenu, whichitem, keycode, numplanes, numshades;
  114.     Rect boundrect;
  115.     DialogPtr dialogptr;
  116.     char *tmpbuff;
  117.     FILE *debugfile;
  118.     locator_measure locmeas;
  119.     pickInformation pickinfo;
  120.    
  121.    /* STARTUP SPHIGS BASED ON USER'S ALLOCATION STRATEGY. */
  122.    printf ("\nPlease enter number of framebuffer planes to allocate:\n");
  123.    scanf ("%d", &numplanes);
  124.    printf ("\nPlease enter number of shades per flexicolor:\n");
  125.    printf ("   (Note: this affects how many different flexicolors will be ");
  126.    printf ("available.)\n");
  127.    scanf ("%d", &numshades);
  128.  
  129.    SPH_begin (514,400, numplanes, numshades);
  130.  
  131.    /* INFORM USER OF FINAL OUTCOME OF HER STRATEGY SPEC. */
  132.    tmpbuff = malloc(1000);
  133.    sprintf (tmpbuff, "I can provide %d flexible colors and %d non-flex colors.",
  134.        NUM_OF_FLEXICOLORS, (NUM_OF_APPL_SETTABLE_COLORS-NUM_OF_FLEXICOLORS));
  135.    ReportSpecialError (tmpbuff, FALSE);
  136.    free (tmpbuff);
  137.  
  138.    SPH_setImplicitRegenerationMode (SUPPRESSED);
  139.  
  140.    /* We hope there are enough flexicolors for these. */
  141.    SPH_loadCommonColor (2, "yellow");
  142.    SPH_loadCommonColor (3, "green");
  143.    SPH_loadCommonColor (4, "turquoise");
  144.    SPH_loadCommonColor (5, "pink");
  145.    SPH_loadCommonColor (6, "goldenrod");
  146.    SPH_loadCommonColor (7, "firebrick");
  147.  
  148.    /* But these need not be flexicolors. */
  149.    SPH_loadCommonColor (red, "red");
  150.    SPH_loadCommonColor (grey, "lightgrey");
  151.    SPH_loadCommonColor (orange, "orange");
  152.    SPH_loadCommonColor (yellow, "yellow");
  153.    SPH_loadCommonColor (limegreen, "yellowgreen");
  154.    SPH_loadCommonColor (forestgreen, "forestgreen");
  155.    SPH_loadCommonColor (blue, "blue");
  156.  
  157.    InitAllViews();
  158.    BuildEverything ();
  159.  
  160.    DisplayAllViews ();
  161.    ShowUsingNewView1();
  162.    SPH_postRoot (NEIGHBORHOOD_STRUCT, PERSPECTIVE_VIEW);
  163.  
  164.    ClearMenuBar();
  165.    MakeMenuBar();
  166.        CheckItem (rendermenu, currenderitem, TRUE);
  167.        CheckItem (viewportmenu, 2, TRUE);
  168.        CheckItem (gridmenu, 1, TRUE);
  169.        CheckItem (chimneymenu, 3, TRUE);
  170.  
  171. #define getmask  (everyEvent)
  172.     
  173.    
  174.    /* INITIALIZE THE GRANULARITY DIALOG BOX */
  175.    DrawDialog (granDialogPtr = GetNewDialog (granDialogID, NULL, (WindowPtr)-1));  
  176.    GetDItem (granDialogPtr, 2, &whichmenu/*dummy*/, 
  177.              &grantextitemhandle, &boundrect/*dummy*/);
  178.    DisplayMotionGranularity();
  179.        
  180.    SetRect (&boundrect, 0,40,3000,3000);
  181.  
  182.    InstallCameraDialog (2);
  183.       
  184.  top:
  185.    if (!stillVirgin)
  186.       SPH_setImplicitRegenerationMode (ALLOWED);
  187.  
  188.    SystemTask();
  189.    GetNextEvent (getmask, &ev);
  190.    if (ev.what == nullEvent) goto top;
  191.    
  192.    if (ev.what == keyDown)
  193.       goto HandleKeyEvent;
  194.       
  195.    SPH_setImplicitRegenerationMode (SUPPRESSED);
  196.    
  197.  sortOfTop:    
  198.    if (IsDialogEvent(&ev))  {
  199.     if (DialogSelect (&ev, &dialogptr, &whichitem)) 
  200.        if (dialogptr == granDialogPtr) {
  201.           switch (whichitem) {
  202.              case 4: if ((motion_granularity += 5) > 99) motion_granularity = 99; break;
  203.              case 5: if ((motion_granularity -= 5) < 1)  motion_granularity =  1; break;
  204.              case 3: InstallHelpDialog (granDialogID+400); break;
  205.           }
  206.           DisplayMotionGranularity();
  207.        }
  208.        else {
  209.          int axis;
  210.          double pitch=0.0, yaw=0.0;
  211.          double bias = 1.0;
  212.          switch (curCamDialogID) {
  213.             case 301:
  214.                axis=whichitem-1;
  215.                if (axis>2) {
  216.                   axis -= 3;
  217.                   bias = -1.0;
  218.                }
  219.            ChangeVRP_relativeUVN (bias*motion_granularity, axis); break;
  220.             case 302:
  221.                axis=whichitem-1;
  222.                if (axis>2) {
  223.                   axis -= 3;
  224.                   bias = -1.0;
  225.                }
  226.            ChangeVRP_relativeWC (bias*motion_granularity, axis); break;
  227.             case 303:
  228.                switch (whichitem) {
  229.                   case 4: pitch = (-motion_granularity); break;
  230.                   case 5: pitch = (+motion_granularity); break;
  231.                   case 2: yaw = (+motion_granularity); break;
  232.                   case 3: yaw = (-motion_granularity); break;
  233.                }
  234.            ChangeVPN (pitch, yaw); break;
  235.             case 304:
  236.                if (whichitem == 3)
  237.                   ChangePRPforOrtho();
  238.                else if (whichitem == 4)
  239.                   ChangePRPforClosestPerspective();
  240.                else
  241.               ChangePRP ((double)((whichitem==1)?motion_granularity:-motion_granularity)); break;
  242.             case 305:
  243.            ChangeFrontClipPlane ((double)((whichitem==1)?motion_granularity:-motion_granularity)); break;
  244.             case 306:
  245.            ChangeBackClipPlane ((double)((whichitem==1)?motion_granularity:-motion_granularity)); break;
  246.      }
  247.      ShowUsingNewView1 ();
  248.       }
  249.       else {  /* DialogSelect returned false */
  250.          if ((ev.what == activateEvt) && ((ev.modifiers & activeFlag) != 0) &&
  251.              (StillDown()) && (FindWindow (ev.where, &winptr) == inContent))
  252.                 if (((DialogPtr)(winptr) == granDialogPtr) ||
  253.                     ((DialogPtr)(winptr) == curCamDialogPtr)) {
  254.                           ev.what = mouseDown;
  255.                          goto sortOfTop;
  256.                }
  257.       }
  258.  
  259.       goto top;
  260.    }
  261.  
  262.  
  263.    switch (ev.what) {
  264.       case updateEvt: goto HandleUpdateEvent; break;       
  265.       case mouseDown: goto HandleMouseEvent; break;
  266.    }
  267.    goto top;
  268.    
  269. /*******/
  270.  
  271. HandleKeyEvent:
  272.    /* only fkeys are to be recognized and processed */
  273.    keycode = (ev.message&keyCodeMask)>>8;
  274.    switch (keycode) {
  275.       case 0x7a: motion_granularity = 3* 1; break;
  276.       case 0x78: motion_granularity = 3* 2; break;
  277.       case 0x63: motion_granularity = 3* 3; break;
  278.       case 0x76: motion_granularity = 3* 4; break;
  279.       
  280.       case 0x60: motion_granularity = 3* 5; break;
  281.       case 0x61: motion_granularity = 3* 6; break;
  282.       case 0x62: motion_granularity = 3* 7; break;
  283.       case 0x64: motion_granularity = 3* 8; break;
  284.       
  285.       case 0x65: motion_granularity = 3* 9; break;
  286.       case 0x6d: motion_granularity = 3*10; break;
  287.       case 0x67: motion_granularity = 3*11; break;
  288.       case 0x6f: motion_granularity = 3*12; break;
  289.       
  290.       case 0x69: motion_granularity = 3*13; break;
  291.       case 0x6b: motion_granularity = 3*14; break;
  292.       case 0x71: motion_granularity = 3*15; break;
  293.    }
  294.    DisplayMotionGranularity ();
  295.    goto top;
  296.  
  297.  
  298. /*******/
  299.  
  300. HandleUpdateEvent:
  301.    if (srgpmac__cwindow == (WindowPtr)(ev.message)) {
  302.          BeginUpdate (srgpmac__cwindow);
  303.          if (stillVirgin) {
  304.             SPH_setImplicitRegenerationMode (ALLOWED);
  305.             stillVirgin = FALSE;
  306.          }
  307.          else
  308.             SPH_regenerateScreen ();
  309.          EndUpdate (srgpmac__cwindow);
  310.    }
  311.    goto top;
  312.  
  313.  
  314. HandleMouseEvent:
  315.    switch (FindWindow (ev.where, &winptr)) {
  316.       case inMenuBar:
  317.          result = MenuSelect (ev.where);
  318.          whichmenu = (result>>16);
  319.          whichitem = (result&0xffff);
  320.          
  321.          if (whichitem == CountMItems(GetMHandle(whichmenu))) {
  322.             InstallHelpDialog (whichmenu);
  323.             HiliteMenu (0);
  324.             break;
  325.          }
  326.          
  327.          switch (whichmenu) {
  328.          
  329.             case pickmenuID:
  330.               tmpdptr = GetNewDialog (25980, NULL, (WindowPtr)-1);
  331.               GetDItem (tmpdptr, 2, &whichmenu/*dummy*/, 
  332.                           &tmpitemhandle, &boundrect/*dummy*/);
  333.               DrawDialog (tmpdptr);
  334.               ClearMenuBar();
  335.                SPH_setInputMode (LOCATOR, EVENT);
  336.                tmpbuff = malloc(500);
  337.                tryagain: SPH_waitEvent (-1);
  338.                SPH_getLocator (&locmeas);
  339.                if (locmeas.buttonChord[LEFT_BUTTON] == UP)
  340.                   goto tryagain;
  341.                if (locmeas.view_index != PERSPECTIVE_VIEW)
  342.                      goto conclude;
  343.                SPH_pickCorrelate (locmeas.position, locmeas.view_index,
  344.                             &pickinfo);
  345.                      
  346.                if (pickinfo.pickLevel == 0) {
  347.                   SysBeep(10);
  348.                   sprintf (tmpbuff, "You picked on nothing.");
  349.              CtoPstr (tmpbuff);
  350.              SetIText (tmpitemhandle, tmpbuff);
  351.                   goto tryagain;
  352.                }
  353.                if (pickinfo.pickLevel == 4)
  354.                   sprintf (tmpbuff, "You picked the chimney on ");
  355.                else
  356.                   sprintf (tmpbuff, "You picked ");
  357.                sprintf (tmpbuff + strlen(tmpbuff), "house #%d ", 
  358.                        1 + (int)((pickinfo.path[1].elementIndex-1)/3));
  359.                sprintf (tmpbuff + strlen(tmpbuff), "on street #%d.", 
  360.                        1 + (int)(pickinfo.path[0].elementIndex/2));
  361.                CtoPstr (tmpbuff);
  362.               SetIText (tmpitemhandle, tmpbuff);
  363.               goto tryagain;
  364.               
  365.              conclude:
  366.                free (tmpbuff);
  367.                DisposDialog (tmpdptr);
  368.                MakeMenuBar();
  369.                break;
  370.                
  371.  
  372.          case chimneymenuID:
  373.             if (whichitem == curchimneymenuitem)
  374.                break;
  375.                CheckItem (chimneymenu, curchimneymenuitem, FALSE);
  376.             if (whichitem == 1) {
  377.                AddChimneyToHouse (HOUSE_STRUCT);
  378.                AddChimneyToHouse (FAKE_HOUSE_TOP_STRUCT);
  379.                AddChimneyToHouse (FAKE_HOUSE_SIDE_STRUCT);
  380.             }
  381.             else if (whichitem == 3) {
  382.                TakeChimneyFromHouse (HOUSE_STRUCT);
  383.                TakeChimneyFromHouse (FAKE_HOUSE_TOP_STRUCT);
  384.                TakeChimneyFromHouse (FAKE_HOUSE_SIDE_STRUCT);
  385.            }
  386.            curchimneymenuitem = whichitem;
  387.                CheckItem (chimneymenu, curchimneymenuitem, TRUE);
  388.                /* BE CAREFUL -- no break should appear just above this line */
  389.                if ( ! ((curHelpDialogID == 4236) || (curHelpDialogID == 4216)))
  390.                   break;  /* careful!!  conditional break! */
  391.                /* CAREFUL -- the modelmenuID case MUST BE NEXT */
  392.                
  393.             
  394.             case modelmenuID:
  395.                /* If we got here, they definitely asked for us to display a dialog */
  396.                InstallHelpDialog (606 + (10*curchimneymenuitem));  /* trick! */
  397.                break;
  398.  
  399.  
  400.             case gridmenuID:
  401.                if (whichitem == curgridmenuitem)
  402.                   break;  /*NOCHANGE*/
  403.                CheckItem (gridmenu, curgridmenuitem, FALSE);
  404.                if (whichitem == 1/*HIDE*/) {
  405.                   SPH_unpostRoot (GRID_STRUCT, PERSPECTIVE_VIEW);
  406.                   SPH_unpostRoot (GRID_STRUCT, TOP_ORTHO_VIEW);
  407.                   SPH_unpostRoot (GRID_STRUCT, SIDE_ORTHO_VIEW);
  408.                }
  409.                else {
  410.                   AddGridInFrontOfRoot (NEIGHBORHOOD_STRUCT, PERSPECTIVE_VIEW);
  411.                   AddGridInFrontOfRoot (FAKE_NEIGHBORHOOD_TOP_STRUCT, TOP_ORTHO_VIEW);
  412.                   AddGridInFrontOfRoot (FAKE_NEIGHBORHOOD_SIDE_STRUCT, SIDE_ORTHO_VIEW);
  413.                }
  414.                curgridmenuitem = whichitem;
  415.                CheckItem (gridmenu, curgridmenuitem, TRUE);
  416.                break;
  417.                
  418.                
  419.                
  420.             case viewportmenuID:
  421.                 if (whichitem == curvpmenuitem)
  422.                    break;  /*NOCHANGE*/
  423.                CheckItem (viewportmenu, curvpmenuitem, FALSE);
  424.                SPH_setImplicitRegenerationMode (SUPPRESSED);
  425.                if (whichitem == 1/*FULL SCREEN*/)
  426.                      DisplayOnlyFullRenderView();
  427.                else
  428.                   DisplayAllViews();
  429.                ShowUsingNewView1();
  430.                SPH_setImplicitRegenerationMode (ALLOWED);
  431.                curvpmenuitem = whichitem;
  432.                CheckItem (viewportmenu, curvpmenuitem, TRUE);
  433.                break;
  434.                   
  435.             case cameramenuID:
  436.                if (whichitem <= 6)
  437.                   InstallCameraDialog (whichitem);
  438.                else if (whichitem == 8) {
  439.                   RestoreCameraToDefault ();
  440.                   ShowUsingNewView1 ();
  441.                }
  442.                else if (whichitem == 10)
  443.                   /* DisplayHelp (whichmenu) */;
  444.                break;
  445.             case rendermenuID:
  446.                switch (whichitem) {
  447.                   case 2:
  448.                      SPH_setRenderingMode (PERSPECTIVE_VIEW, WIREFRAME_RAW); break;
  449.                   case 5:
  450.                      SPH_setRenderingMode (PERSPECTIVE_VIEW, WIREFRAME); break;
  451.                   case 6:
  452.                      SPH_setRenderingMode (PERSPECTIVE_VIEW, LIT_FLAT); break;
  453.                }
  454.                CheckItem (rendermenu, currenderitem, FALSE);
  455.                currenderitem = whichitem;
  456.                CheckItem (rendermenu, currenderitem, TRUE);
  457.                break;
  458.          }
  459.          HiliteMenu (0);
  460.          break;
  461.       case inDrag:
  462.          DragWindow (winptr, ev.where, &boundrect);
  463.          break;
  464.       case inContent:
  465.          SelectWindow (winptr);
  466.          break;
  467.       case inGoAway:
  468.          if (TrackGoAway (winptr, ev.where)) {
  469.             if (winptr == srgpmac__cwindow) {
  470.                SPH_end();
  471.                ExitToShell();
  472.             }
  473.             else if (winptr == curHelpDialogPtr) {
  474.                DisposDialog (curHelpDialogPtr);
  475.                curHelpDialogID = 0;
  476.             }
  477.          }
  478.          break;
  479.       case inSysWindow:
  480.          SystemClick (&ev, winptr);
  481.          break;
  482.    }
  483.    goto top;
  484. }
  485.  
  486.